package com.sofwin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.dto.UserDto;
import com.sofwin.mapper.SysUserMapper;
import com.sofwin.pojo.SysUser;
import com.sofwin.pojo.SysUserExample;
import com.sofwin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private SysUserMapper mapper;
    @Override
    public boolean saveUsers(List<SysUser> users) {
        boolean status = true;
        int flag=0;
            for (SysUser user : users) {
                flag+=mapper.insertSelective(user);
            }
        return flag==users.size()?true:false;
    }

    @Override
    public SysUser selectUser(SysUser user) {
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();
        if(user!=null){
            if(user.getLoginAccount()!=null && !"".equals(user.getLoginAccount())){
                criteria.andLoginAccountEqualTo(user.getLoginAccount());
            }
            if(user.getLoginPwd()!=null && !"".equals(user.getLoginPwd())){
                criteria.andLoginPwdEqualTo(user.getLoginPwd());
            }
        }
        List<SysUser> users = mapper.selectByExample(example);
        if(users!=null && users.size()>0){
            return users.get(0);
        }
        return null;
    }

    @Override
    public boolean checkLogin(SysUser user) {
        boolean status = false;
        SysUser sysUser = selectUser(user);
        if(sysUser!=null){
            status = true;
        }
        return status;
    }

    @Override
    public PageInfo<UserDto> selectUserByPage(SysUser user, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<UserDto> users = mapper.selectUsers(user);
        return new PageInfo<UserDto>(users);
    }

    @Override
    public boolean deleteByIds(Integer[] ids) {
        int flag =0;
        for(Integer id:ids){
            flag+=mapper.deleteByPrimaryKey(id);
        }
        return flag==ids.length?true:false;
    }

    @Override
    public boolean saveUser(SysUser user) {
        if(user.getId()!=null){
            return mapper.updateByPrimaryKeySelective(user)==1?true:false;
        }
        return mapper.insertSelective(user)==1?true:false;
    }

    @Override
    public SysUser selectUserById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean selectUserByName(String userName) {
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();
        criteria.andLoginAccountEqualTo(userName);
        List<SysUser> users = mapper.selectByExample(example);
        if(users!=null && users.size()==1){
            return true;
        }
        return false;
    }

    @Override
    public List<String> selectUserPermssionsByRoleId(Integer roleId) {
        return mapper.selectRolePermissions(roleId);
    }

    @Override
    public void saveMonitor(Integer id, String currDate, String url, long request_date, String ip) {
        mapper.insertMonitor(id,url,currDate,request_date,ip);
    }

}
